// 基础类型定义
export interface BaseEntity {
  id: string
  createdAt: Date
  updatedAt: Date
}

// 聊天相关类型
export interface ChatMessage {
  id: string
  sessionId: string
  role: 'user' | 'assistant' | 'system'
  content: string
  type: 'text' | 'task' | 'system' | 'error'
  timestamp: Date
  metadata?: Record<string, any>
  isStreaming?: boolean
}

export interface ChatSession extends BaseEntity {
  title: string
  messages: ChatMessage[]
  status: 'active' | 'completed' | 'error'
  metadata?: Record<string, any>
}

// 任务相关类型
export type TaskType = 'RESEARCH' | 'ANALYSIS' | 'CALCULATION' | 'REASONING' | 'SYNTHESIS'
export type TaskStatus = 'PENDING' | 'IN_PROGRESS' | 'COMPLETED' | 'FAILED' | 'CANCELLED'
export type AgentType = 'GENERAL_QA' | 'RESEARCH_SPECIALIST' | 'MATH_SOLVER' | 'LOGIC_REASONER'

export interface Task extends BaseEntity {
  sessionId: string
  description: string
  type: TaskType
  status: TaskStatus
  priority: number
  sequence: number
  dependencies: string[]
  agentType: AgentType
  estimatedTime: number
  actualTime?: number
  progress: number
  result?: TaskResult
  error?: TaskError
  mcpServices?: MCPServiceCall[]
  startedAt?: Date
  completedAt?: Date
}

export interface TaskResult {
  content: string
  confidence: number
  sources?: Array<{
    type: string
    url: string
    title: string
    reliability: number
  }>
  metadata?: Record<string, any>
}

export interface TaskError {
  code: string
  message: string
  details?: Record<string, any>
  retryable: boolean
}

export interface TaskModification {
  type: 'add' | 'delete' | 'modify' | 'reorder'
  taskId?: string
  newTask?: Partial<Task>
  newOrder?: string[]
  reason?: string
}

// 执行状态类型
export interface ExecutionStatus {
  sessionId: string
  executionId: string
  status: 'QUEUED' | 'RUNNING' | 'COMPLETED' | 'FAILED' | 'TIMEOUT' | 'CANCELLED'
  progress: ExecutionProgress
  timing: ExecutionTiming
  resources: ExecutionResources
  logs: ExecutionLog[]
}

export interface ExecutionProgress {
  overall: number
  currentTask?: {
    id: string
    progress: number
    estimatedRemaining: number
  }
  completed: number
  total: number
  failed: number
}

export interface ExecutionTiming {
  startTime: Date
  estimatedEndTime?: Date
  actualEndTime?: Date
  totalTime?: number
  averageTaskTime?: number
}

export interface ExecutionResources {
  agentsUsed: number
  mcpServicesCalled: number
  memoryUsage: number
  cpuUsage: number
}

export interface ExecutionLog {
  id: string
  timestamp: Date
  level: 'INFO' | 'WARNING' | 'ERROR'
  message: string
  taskId?: string
  details?: Record<string, any>
}

export interface TimelineItem {
  id: string
  title: string
  description?: string
  timestamp: string
  type?: 'primary' | 'success' | 'warning' | 'danger' | 'info'
  color?: string
  icon?: any
  size?: 'normal' | 'large'
  metadata?: Record<string, any>
}

// MCP服务类型
export interface MCPService extends BaseEntity {
  name: string
  description: string
  endpoint: string
  methods: string[]
  status: 'active' | 'inactive' | 'error'
  healthCheck?: {
    lastCheck: Date
    status: 'healthy' | 'unhealthy'
    responseTime: number
  }
  metadata?: Record<string, any>
}

export interface MCPServiceCall {
  serviceId: string
  method: string
  parameters?: Record<string, any>
  callTime: Date
  responseTime?: number
  result?: any
  error?: string
}

// SSE响应类型
export type SSEObjectType = 
  | 'chat.completion.chunk'
  | 'chat.completion'
  | 'tasks.custom.chunk'
  | 'task_run.custom.chunk'
  | 'task_result.custom.chunk'
  | 'session.custom.chunk'
  | 'agent.custom.chunk'
  | 'system.custom.chunk'
  | 'mcp.service.chunk'
  | 'mcp.result.chunk'
  | 'error.custom.chunk'
  | 'warning.custom.chunk'
  | 'confirmation.custom.chunk'

export interface SSEEvent {
  object: SSEObjectType
  id?: string
  model?: string
  created?: number
  choices?: Array<{
    delta?: {
      content?: string
      role?: string
    }
    index: number
    finish_reason?: string
  }>
  custom?: Record<string, any>
}

// API请求类型
export interface ChatCompletionRequest {
  messages: Array<{
    role: 'user' | 'assistant' | 'system'
    content: string
  }>
  stream: boolean
  session_id?: string
  custom?: {
    priority?: 'low' | 'normal' | 'high'
    language?: string
    include_reasoning?: boolean
    max_task_count?: number
    execution_mode?: 'auto' | 'manual'
    timeout?: number
  }
}

// UI状态类型
export interface UIState {
  theme: 'light' | 'dark'
  sidebarCollapsed: boolean
  activePanel: 'chat' | 'tasks' | 'mcp' | 'settings'
  showTaskPanel: boolean
  showMCPPanel: boolean
  windowSize: {
    width: number
    height: number
  }
  panelSizes: {
    chat: number
    task: number
  }
  isFullscreen: boolean
  loading: {
    global: boolean
    chat: boolean
    tasks: boolean
    mcp: boolean
  }
}

// 表单验证类型
export interface ValidationRule {
  required?: boolean
  min?: number
  max?: number
  pattern?: RegExp
  validator?: (value: any) => boolean | string
  message?: string
}

export interface FormField {
  name: string
  label: string
  type: 'text' | 'textarea' | 'number' | 'select' | 'switch'
  rules?: ValidationRule[]
  options?: Array<{ label: string; value: any }>
  placeholder?: string
  disabled?: boolean
}

// 事件类型
export interface AppEvent {
  type: string
  payload?: any
  timestamp: Date
  source?: string
}

// 错误类型
export interface AppError {
  code: string
  message: string
  details?: Record<string, any>
  timestamp: Date
  stack?: string
}

// 配置类型
export interface AppConfig {
  api: {
    baseURL: string
    timeout: number
    retryCount: number
  }
  sse: {
    endpoint: string
    reconnectInterval: number
    maxReconnectAttempts: number
  }
  ui: {
    theme: 'light' | 'dark'
    language: 'zh-CN' | 'en-US'
    animations: boolean
    sounds: boolean
  }
  chat: {
    maxMessageLength: number
    autoScroll: boolean
    showTimestamp: boolean
    enableVoiceInput: boolean
  }
  tasks: {
    maxTaskCount: number
    defaultTimeout: number
    showDependencies: boolean
    autoConfirm: boolean
  }
}